package com.glsc.ngateway.opmanage.service.outsys;


import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemAuthTemplate;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.outsys.OutSystemAuthTemplateRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@Slf4j
public class OutSystemAuthTemplateService {

    @Resource
    private OutSystemAuthTemplateRepository authTemplateRepository;

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<OutSystemAuthTemplate> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<OutSystemAuthTemplate> page = authTemplateRepository.findAll(SpecificationUtil.buildSpecification(param, OutSystemAuthTemplate.class), pageable);
        return page;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public OutSystemAuthTemplate save(OutSystemAuthTemplate obj) {
        return authTemplateRepository.save(obj);
    }

    public List<OutSystemAuthTemplate> saveAll(List<OutSystemAuthTemplate> list) {
        return authTemplateRepository.saveAll(list);
    }


    /**
     * 根据地查找
     *
     * @param id
     * @return
     */
    public OutSystemAuthTemplate findById(Integer id) {
        Optional<OutSystemAuthTemplate> optional = authTemplateRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteById(Integer id) {
        authTemplateRepository.deleteById(id);
    }


    /**
     * 根据系统id获取所有的系统基准权限信息
     *
     * @param systemId
     * @return
     */
    public List<OutSystemAuthTemplate> findBySystemId(Integer systemId) {
        return authTemplateRepository.findAll(Example.of(OutSystemAuthTemplate.builder().sysId(systemId).build()));
    }

    /**
     * 根据id集合查找
     *
     * @param ids
     * @return
     */
    public List<OutSystemAuthTemplate> findByIds(List<Integer> ids) {
        return authTemplateRepository.findAllById(ids);
    }


}
